﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class LoginPage_HotelRegitser : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button_more_Click(object sender, EventArgs e)
    {

        Calendar1.Visible = Calendar1.Visible ? false : true;
    }
    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {
        TextBox_birthday.Text = Calendar1.SelectedDate.ToShortDateString();
    }
    protected void Button_regitser_Click(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            return;
        }

         Label_hotelname_note.Visible = false;
         Label_account_note.Visible = false;
         Label_hoteladdress_note.Visible = false;

         SqlConnection sqlcon = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnString"].ToString());
         DateTime datetime1;

         if (!DateTime.TryParse(TextBox_birthday.Text, out datetime1))
         {
             Label_birtdday_note.Visible = true;
             return;
         }
         else
         {
             Label_birtdday_note.Visible = false;
         }

        string sex="0";
        if(RadioButton_female.Checked==true)
             sex = "1";

        if (TextBox_account.Text.Equals(""))
        {
            Label_account_note.Visible = true;
            Label_account_note.Text = "账户不能为空！";

            return;
        }
        if (TextBox_address.Text.Equals(""))
        {
            Label_hoteladdress_note.Visible = true;
          

            return;
        }


        //User user = new User(TextBox_IDCard.Text, TextBox_name.Text, sex, TextBox_birthday.Text);
        Hotel hotel = new Hotel(
            TextBox_hotelname.Text,
            DropDownList_pro.SelectedValue,
            DropDownList_city.SelectedValue,
            DropDownList_county.SelectedValue,
            TextBox_address.Text,
            TextBox_phone.Text
            );

        //HotelUser hotelUser = new HotelUser(user, hotel, TextBox_account.Text, TextBox_psw1.Text, "0");
        HotelUser hotelUser = new HotelUser(hotel,TextBox_IDCard.Text, TextBox_name.Text, sex, TextBox_birthday.Text ,TextBox_account.Text, TextBox_psw1.Text, "0");

        bool insertOk = false;
        sqlcon.Open();
        
        

        int userDataStatus = hotelUser.compareToData(sqlcon);
        Label_idcard_note.Visible = false;
        if (userDataStatus == 1)
        {
            Label_idcard_note.Text = "用户信息有误，请仔细核对身份信息！";
            Label_idcard_note.Visible = true;
            return;
        }
        
     

       
      
      


       if (HotelUser.isExist(sqlcon, TextBox_account.Text))
       {
           Label_account_note.Visible = true;
           Label_account_note.Text = "账户已经存在！";
           sqlcon.Close();
           return;
       }
       else
       {
           Label_account_note.Visible = true;
           Label_account_note.Text = "账户可用";
       }
       
       if (Hotel.isExist(sqlcon, TextBox_hotelname.Text,DropDownList_pro.SelectedValue,DropDownList_city.SelectedValue,DropDownList_county.SelectedValue))
       {
           Label_hotelname_note.Visible = true;
           Label_hotelname_note.Text = "宾馆已经被注册！";
           sqlcon.Close();
           return;
       }
       SqlFormat.beginTran(sqlcon);

        //ToDo(xu):插入数据
       if (hotelUser.insert(sqlcon)!=null)
       {
           insertOk = true;
        }
  
       


        if (insertOk)
        {
            SqlFormat.commit(sqlcon);

            Response.Redirect("RegisterOk.aspx");
        }
        else
        {
            SqlFormat.rollback(sqlcon);
        }

        sqlcon.Close();
    }
    protected void TextBox_account_TextChanged(object sender, EventArgs e)
    {
  
    }
}